/*
 * Copyright 2007-2009 Perttu "celeron55" Ahola <celeron55@gmail.com>
 *
 * This file is part of jukumagic.
 *
 * jukumagic is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * jukumagic is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with jukumagic.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "common.h"
#include "rect.h"

Rect::Rect()
{
	m_spec = RECT_SPECIFY_DEFAULT;
}

Rect::Rect(Rect &rect)
{
	BoardRect::operator=((BoardRect)rect);
	m_spec = rect.m_spec;
}

Rect::Rect(BoardRect r)
{
	BoardRect::operator=(r);
}

Rect::~Rect()
{
}

/*void Rect::operator = (BoardRect r)
{
}*/

JukuObject* Rect::MakeACopy()
{
    Rect *rect = new Rect(*this);
    return rect;
}

void Rect::WriteXML(TiXmlNode* root)
{
    TiXmlElement *element = new TiXmlElement("Rect");
    root->LinkEndChild(element);
    //element->SetAttribute("name", m_name.mb_str(wxConvUTF8));
    if(m_spec == RECT_SPECIFY_WH){
		element->SetAttribute("x", m_x);
		element->SetAttribute("y", m_y);
		element->SetAttribute("w", m_w);
		element->SetAttribute("h", m_h);
   	}
	else{
		element->SetAttribute("x1", m_x);
		element->SetAttribute("y1", m_y);
		element->SetAttribute("x2", m_x+m_w);
		element->SetAttribute("y2", m_y+m_h);
	}
	WriteBaseXML(element);
}

JukuObject* Rect::ReadFromXMLElement(TiXmlElement* element)
{
    Rect *rect = new Rect();

    if(!element->Attribute("x", &(rect->m_x))) element->Attribute("x1", &(rect->m_x));
    if(!element->Attribute("y", &(rect->m_y))) element->Attribute("y1", &(rect->m_y));
    
    bool was_w, was_h;
    was_w = element->Attribute("w", (int*)&(rect->m_w)) != NULL;
    was_h = element->Attribute("h", (int*)&(rect->m_h)) != NULL;
    if(!was_w){
    	int x2;
    	if(element->Attribute("x2", &x2)){
    		if(x2 > rect->m_x){
				rect->m_w = x2 - rect->m_x;
			}
			else{
				rect->m_x = x2;
				rect->m_w = rect->m_x - x2;
			}
		}
	}
    if(!was_h){
    	int y2;
    	if(element->Attribute("y2", &y2)){
    		if(y2 > rect->m_y){
				rect->m_h = y2 - rect->m_y;
			}
			else{
				rect->m_y = y2;
				rect->m_h = rect->m_y - y2;
			}
		}
	}
	
	if(!was_w && !was_h) rect->m_spec = RECT_SPECIFY_X2Y2;
	else               rect->m_spec = RECT_SPECIFY_WH;
	
	rect->ReadBaseXML(element);

    return rect;
}


void Rect::Draw(wxDC &dc, wxWindow *window, wxPoint origin)
{
	//std::cout<<"Rect::Draw()"<<std::endl;
	dc.DrawRectangle(m_x + origin.x, m_y + origin.y, m_w, m_h);
}

BoardRect Rect::GetDrawArea()
{
	return BoardRect(m_x - RECT_MAX_LINE_WIDTH / 2, m_y - RECT_MAX_LINE_WIDTH / 2, 
			m_w + RECT_MAX_LINE_WIDTH, m_h + RECT_MAX_LINE_WIDTH);
}

void Rect::Move(int dx, int dy)
{
	m_x += dx;
	m_y += dy;
}

void Rect::Rotate(TurnDirection dir, BoardPoint origin)
{
	int ox1 = m_x - origin.m_x;
	int oy1 = m_y - origin.m_y;
	int ox2 = m_x+m_w - origin.m_x;
	int oy2 = m_y+m_h - origin.m_y;
	int nox1, noy1, nox2, noy2;
	if(dir==CCW){
		nox1 = oy1;
		nox2 = oy2;
		noy1 = -ox1;
		noy2 = -ox2;
	}
	else{ //CW
		nox1 = -oy1;
		nox2 = -oy2;
		noy1 = ox1;
		noy2 = ox2;
	}
	int nx1 = origin.m_x + nox1;
	int ny1 = origin.m_y + noy1;
	int nx2 = origin.m_x + nox2;
	int ny2 = origin.m_y + noy2;
	if(nx1 < nx2){ m_x = nx1; m_w = nx2-nx1; }
	else         { m_x = nx2; m_w = nx1-nx2; }
	if(ny1 < ny2){ m_y = ny1; m_h = ny2-ny1; }
	else         { m_y = ny2; m_h = ny1-ny2; }
}

